package wendyeq.iweb.blog.dao.impl;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import wendyeq.iweb.blog.pojo.UserAccount;
import wendyeq.iweb.blog.util.JdoManager;
import wendyeq.iweb.blog.dao.IUserAccountDAO;

@Repository("userAccountDAO")
public class UserAccountDAO implements IUserAccountDAO {
	private static final Logger logger = Logger.getLogger(UserAccountDAO.class);
	
	@SuppressWarnings("unchecked")
	public UserAccount findByName(String un){
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(UserAccount.class);
		query.setFilter("username == un");
		query.declareParameters("String un");
		try {
			List<UserAccount> ual=(List<UserAccount>)query.execute(un);
			if (ual.size() >0) {
				return ual.get(0);
			} else {
				return null;
			}
		} catch (Exception e) {
			logger.error(e);
			return null;
		}finally{
			query.closeAll();
			manager.close();
		}
		

	}

	@Override
	public boolean delete(String id) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 存储用户
	 * */
	@Override
	public boolean saveOrUpdate(UserAccount ua) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			manager.makePersistent(ua);
			logger.info("a"+ua.getId());
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally{
			manager.close();
		}
		return true;
	}

	@Override
	public List<UserAccount> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserAccount findById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserAccount> findByPage(int min, int max) {
		// TODO Auto-generated method stub
		return null;
	}


}
